var sesionClass = {
    parent: "parentId",
    pattern: "4H",
    win: null,
    formA: null,
    formB: null,
    grid: null,
    actionBar: null,
    layout: null,
    dhxCombo: null,
    BoleCod: null,
    PregCod: null,
    statusGrid: null,
    layout2:null,
    ruta:null,
    dhxCalendar1: null,
    dhxCalendar2: null,
    fecha_inicio: null,
    fecha_fin: null,
    fecha1:null,
    fecha2:null,
    me2:null,
    widgetWin: function(){
        var windows = new dhtmlXWindows();
        this.win = windows.createWindow('window_1', 0, 0, 670, 400);
        this.win.setText('Sesiones de Usuarios');
        this.win.clearIcon();
        this.win.button('park').hide();
        this.win.button('minmax1').hide();
        this.win.denyResize();
        this.win.setModal(1);
        this.win.centerOnScreen();

        this.widgetBar();
        this.layout = this.win.attachLayout('3U');

    },
    widgetBar: function(){
        this.actionBar =  this.win.attachToolbar();
        this.actionBar.setIconsPath(mainPath+'/images/icons32/');
        this.actionBar.setIconSize(32);
        this.actionBar.loadXML(mainPath+"xml/toolbarsesion.xml");
        this.actionBar.setAlign('left');
    },

    widgetGrid: function(){
         var cell = this.layout.cells('c');
        //cell.setWidth('700');
        cell.setHeight('230');
        cell.hideHeader();
       // cell.fixSize(true, true);
        var me=this;
        me.statusGrid = cell.attachStatusBar();
        me.grid = cell.attachGrid();
        me.grid.setHeader("Id,Fecha Ingreso,Pais,Localidad,IP Ingreso,Hora Ingreso,Ususario,Rol");
        me.grid.setColTypes("ro,ro,ro,ro,ro,ro,ro,ro");
        //me.grid.enableMultiline(true);
        me.grid.setColAlign("left,left,left,left,left,left,left");
        me.grid.setInitWidths("30,0,70,*,*,*,*,0");
        me.grid.init();
        var f=new Date();
        var fechaI=(f.getFullYear()+"-"+(f.getMonth()+1)+"-"+f.getDate());
        me.grid.load(mainPath+"index.php/Acces/Grid_data?fecha_inicio="+ fechaI+"&fecha_fin="+fechaI, function(){
            me.grid.groupBy(1);
            me.statusGrid.setText("Numero de Registros: "+me.grid.getRowsNum());
        },"json");


    },
    widgetFormB: function(){
        var panel2 = this.layout.cells('b');
        panel2.hideHeader();
        var str = [
            { type:"input" , name:"Usuario", label:"Nombre Usuario:",labelWidth:100,  inputWidth:350, labelLeft:20,position:"absolute",inputTop:40 ,labelTop:17,inputLeft:20 }
        ];
        me2 = this;
        me2.formB = panel2.attachForm(str);
        var usuario=me2.formB.getItemValue("Usuario");
        me2.formB.getInput("Usuario").onkeyup = function (keyValue) {
            me2.grid.filterBy(6,keyValue.target.value.replace(/^\s*|\s*$/g, ""));
            me2.grid.groupBy(1);
            me2.statusGrid.setText("Numero de Registros: "+me2.grid.getRowsNum());
        };
    },
    widgetFormA: function(){
        var panel = this.layout.cells('a');
        panel.setWidth('220');
        panel.hideHeader();
        var str = [
            { type:"hidden", name:"usu1"},
            { type:"label" , name:"label_titulo",label:"<b>RANGO DE FECHAS</b>",position:"absolute",labelLeft:50,labelTop:0,labelWidth:150},
            { type:"label" , name:"label_inicio",label:"Inicio:",position:"absolute",labelLeft:5,labelTop:17,labelWidth:150},
            { type:"calendar" , name:"fecha_inicio",validate:"NotEmpty",position:"absolute",inputTop:22,inputLeft:52, dateFormat:"%Y-%m-%d",readonly:true, options:{}},
            { type:"label" , name:"label_fin",label:"Final:",position:"absolute",labelLeft:5,labelTop:47,labelWidth:150},
            { type:"calendar" , name:"fecha_fin",validate:"NotEmpty",position:"absolute",inputTop:52,inputLeft:52, dateFormat:"%Y-%m-%d",readonly:true, options:{}}
        ];
        var me = this;
        me.formA = panel.attachForm(str);
        dhxCalendar1 = me.formA.getCalendar("fecha_inicio");
        dhxCalendar2 = me.formA.getCalendar("fecha_fin");
        fecha_inicio=me.formA.getItemValue("fecha_inicio");
        fecha_fin=me.formA.getItemValue("fecha_fin");
        var myEvent1 = dhxCalendar1.attachEvent("onClick", function (){
            fecha_inicio=me.formA.getItemValue("fecha_inicio");
            fecha1=(fecha_inicio .getFullYear()+"-"+(fecha_inicio .getMonth()+1)+"-"+fecha_inicio .getDate());
            me.grid.clearAll();
        });
        var myEvent1 = dhxCalendar2.attachEvent("onClick", function (){
            fecha_fin=me.formA.getItemValue ("fecha_fin");
            fecha2=(fecha_fin.getFullYear()+"-"+(fecha_fin.getMonth()+1)+"-"+fecha_fin.getDate());
            me.grid.load(mainPath+"index.php/Acces/Grid_data?fecha_inicio="+ fecha1+"&fecha_fin="+fecha2, function(){
                me.grid.groupBy(1);
                me.statusGrid.setText("Numero de Registros: "+me.grid.getRowsNum());
            },"json");
        });
        //me.grid.init();
    },


    widgetMain: function(){
        var me=this;
        me.widgetWin();
        me.widgetGrid();
        me.widgetFormB();
        me.widgetFormA();
           me.actionBar.attachEvent("onClick", function(itemId){
            switch(itemId){
                case 'btn_print':
                    if(me.grid.getRowsNum()>0){
                        open(mainPath+"index.php/reports3/Acces?fecha_inicio="+fecha1+"&fecha_fin="+fecha2+"&usu1="+me.formB.getItemValue("Usuario"));
                    }
                    break;
                case 'btn_clear':
                    me.grid.clearAll();


            }
        });

    }
};

